package com.example.demo;

import com.example.demo.dao.EmpDao;
import com.example.demo.dao.StudentDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
		DataSourceTransactionManagerAutoConfiguration.class,
		JdbcTemplateAutoConfiguration.class})
@Slf4j
public class DemoApplication implements CommandLineRunner {

	@Autowired
	EmpDao empDao;
	@Autowired
	StudentDao studentDao;

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Bean
	@ConfigurationProperties("stu.datasource")
	public DataSourceProperties studentProperties(){
		return new DataSourceProperties();
	}
	@Bean
	public DataSource studentDataSource(){
		DataSourceProperties p = studentProperties();
		log.info("emp url {}",p.getUrl());
		return p.initializeDataSourceBuilder().build();
	}

	@Bean
	@Resource
	public PlatformTransactionManager studentTx(DataSource studentDataSource){
		return new DataSourceTransactionManager(studentDataSource);
	}
	@Bean
	@Resource
	@Primary
	public JdbcTemplate jdbcTemplate(DataSource studentDataSource){
		return new JdbcTemplate(studentDataSource);
	}
	@Bean
	@ConfigurationProperties("emp.datasource")
	public DataSourceProperties empProperties(){
		return new DataSourceProperties();
	}
	@Bean
	public DataSource empDataSource(){
		DataSourceProperties p = empProperties();
		log.info("emp url {}",p.getUrl());
		return p.initializeDataSourceBuilder().build();
	}

	@Bean
	@Resource
	public PlatformTransactionManager empTx(DataSource empDataSource){
		return new DataSourceTransactionManager(empDataSource);
	}
	@Bean("empTemplate")
	@Resource
	public JdbcTemplate empJdbcTemplate(DataSource empDataSource){
		return new JdbcTemplate(empDataSource);
	}
	@Override
	public void run(String... args) throws Exception {
		studentDao.studenOpt();
		empDao.empOpt();
	}



}
